package com.changfang.gold_man.service.Imp;

import com.changfang.gold_man.entity.Car;
import com.changfang.gold_man.mapper.CarMapper;
import com.changfang.gold_man.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CarServiceImpl implements CarService {
    @Autowired
    private CarMapper carMapper;
    //查询所有车辆
    @Override
    public List<Car> findAll() {
        List<Car> carList = carMapper.findAll();
        return carList;
    }
    //注册车辆
    @Override
    public void register(String user_id,String car_type, String car_address,String car_img,String car_gold) {
        carMapper.register(user_id,car_type,car_address,car_img,"1",Float.parseFloat(car_gold));
    }



    //通过用户id查找车辆
    @Override
    public List<Car> findByUid(String user_id) {
        List<Car> carList = carMapper.findByUid(user_id);
        return carList;
    }
//    删除车辆
    @Override
    public void delete(String car_id) {
        carMapper.delete(Integer.parseInt(car_id));
    }
//车ID查车辆
    @Override
    public Car findByCarId(String car_id) {
        try{
            Integer carIdInt =  Integer.parseInt(car_id);
            Car car =  carMapper.findByCarId(carIdInt);
            return car;
        }catch (Exception e){
            return null;
        }
    }
    //修改车辆可用状态
    @Override
    public String updateStatu(Map<String,String> car_info) {
        try {
            String car_id = car_info.get("car_id");
            String car_statu = car_info.get("car_statu");
            carMapper.updateStatu(Integer.parseInt(car_id),car_statu);
            return "success";
        }catch (Exception e){
            return "error";
        }
    }
    //修改车辆借用者
    @Override
    public String updatePerson(String car_id,String person_id){
        try {
            carMapper.updatePerson(Integer.parseInt(car_id), person_id);
            return "success";
        }catch (Exception e){
            return "error";
        }
    }
    //查找我借用的车辆
    @Override
    public List<Car> findPerson(String person_id) {
        try {
            List<Car> carList = carMapper.findPerson(person_id);
            return carList;
        }catch (Exception e){
            return null;
        }

    }
    //归还车俩
    @Override
    public String returnCar(String car_id) {
        try {
            Map<String,String> car_info = new HashMap();
            car_info.put("car_id",car_id);
            car_info.put("car_statu","1");
            updateStatu(car_info);
            updatePerson(car_id,null);
            return "OK";
        }catch (Exception e){
            return "ERROR";
        }
    }
}
